package com.project.web.wechat.controller.app.cpWeChat;

import com.google.gson.Gson;
import com.project.common.core.utils.exception.Result;
import com.project.web.config.WxCorp.WxCpConfig;
import com.project.web.config.WxCorp.WxCpConfiguration;
import com.project.web.util.exception.WeChatCustomException;
import com.project.web.util.exception.WECHAT_RESP_CODE_ENUM;
import com.project.web.wechat.controller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.cp.api.WxCpService;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 企业微信基控制器
 *
 * @author wyy
 * @date 2020-06-24
 */
@Api(tags = "JS-SDK接口")
@RestController("workWxBaseController")
@RequestMapping("/weChat/auth/workWx/base")
public class WorkWxBaseController extends BaseController {

    /**
     * 获取JS—SDK签名
     *
     * @param pageUrl 签名页面URl
     * @return
     */
    @ApiOperation("获取JS-SDK config签名")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageUrl", value = "网页路径", required = true, dataType = "String")
    })
    @PostMapping(value = "/getJsSdkSignature")
    public Result<Map<String, WxJsapiSignature>> getJsSdkSignature(String pageUrl, HttpServletRequest request) {
        try {
            pageUrl = URLDecoder.decode(pageUrl, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 企业微信基本参数
        Map<Object, Object> returnMap = new HashMap<>();
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());

        // 企业签名
        try {
            WxJsapiSignature jsApiSignature = wxCpService.createJsapiSignature(pageUrl);
            returnMap.put("jsApiSignature", jsApiSignature);
        } catch (Exception e) {
            log.error("企业JS-SDK签名错误:{}", ExceptionUtils.getStackTrace(e));
            throw new WeChatCustomException(WECHAT_RESP_CODE_ENUM.SIGNATURE_FAIL);
        }

        // 初始化返回参数
        log.info("\r\n返回参数:{}", new Gson().toJson(returnMap));
        return getResult(returnMap);
    }

    /**
     * 获取agentJS—SDK签名
     *
     * @param pageUrl 签名页面URl
     * @return
     */
    @ApiOperation(value = "获取JS-SDK agent config签名")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageUrl", value = "网页路径", required = true, dataType = "String")
    })
    @PostMapping(value = "/getAgentJsSdkSignature")
    public Result<Map<String, WxJsapiSignature>> getAgentJsSdkSignature(String pageUrl, HttpServletRequest request) {
        try {
            pageUrl = URLDecoder.decode(pageUrl, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 企业微信基本参数
        Map<Object, Object> returnMap = new HashMap<>();
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());

        // 企业应用签名
        try {
            WxJsapiSignature agentJsApiSignature = wxCpService.createJsapiSignature(pageUrl);
            returnMap.put("agentJsApiSignature", agentJsApiSignature);
        } catch (Exception e) {
            log.error("企业应用JS-SDK签名错误:{}", ExceptionUtils.getStackTrace(e));
            throw new WeChatCustomException(WECHAT_RESP_CODE_ENUM.SIGNATURE_FAIL);
        }

        // 初始化返回参数
        returnMap.put("agentId", wxCpConfig.getAgentId());
        return getResult(returnMap);
    }
}
